package com.qqtech.qquan.vote.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.circle.enums.CircleAuditStatusEnum;
import com.qqtech.qquan.circle.model.CircleBaseVo;
import com.qqtech.qquan.circle.model.CircleMemberVo;
import com.qqtech.qquan.circle.service.CircleBaseService;
import com.qqtech.qquan.circle.service.CircleMemberService;
import com.qqtech.qquan.vote.model.VoteBase;
import com.qqtech.qquan.vote.model.VoteBaseQuery;
import com.qqtech.qquan.vote.model.VoteBaseVo;
import com.qqtech.qquan.vote.service.VoteBaseService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/vote/base")
public class VoteBaseController extends BaseWebController<VoteBase, VoteBaseQuery> {
	@Resource
	private VoteBaseService voteBaseService;
	
	@Resource
	private CircleMemberService circleMemberService;
	
	@Resource
	private CircleBaseService circleBaseService;
    
	@Override
	protected BaseService<VoteBase> getBaseService() {
		return voteBaseService;
	}
	
	/**
	 * 新增
	 * 
	 * @param voteBaseVo
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/create")
	@ResponseBody
	public Result create(@RequestBody VoteBaseVo voteBaseVo) {
		Result result = new Result();
		Integer curUserId = super.getUserId();
		if(null!=voteBaseVo.getCircleId()){
			CircleMemberVo circleMemberVo=circleMemberService.getCircleMember(curUserId, voteBaseVo.getCircleId());
			if(YesNoEnum.NO.getCode()==circleMemberVo.getCanAddVote()){//是否可以添加圈子投票
				result.addErr("您在该圈子未获得发布投票权限.");
				return result;
			}
			CircleBaseVo circleBaseVo=circleBaseService.getById(voteBaseVo.getCircleId());
			if(circleBaseVo.getAuditStatus()!=CircleAuditStatusEnum.AUDIT_PASS.getCode()){
				result.addErr("该圈子未审核通过。");
				return result;
			}
		}
		voteBaseVo.setCreatorId(curUserId);
		Integer voteId=voteBaseService.addVoteBase(voteBaseVo);
		result.put("voteId", voteId);
		return result;
	}
}
